package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.UacGroupIpSegmentMapper;
import com.csun.cmny.provider.model.domain.UacGroupIpSegment;
import com.csun.cmny.provider.model.vo.ipauth.IpSegmentVo;
import com.csun.cmny.provider.service.UacGroupIpSegmentService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-09-30 14:29
 * @Description:
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class UacGroupIpSegmentServiceImpl extends BaseService<UacGroupIpSegment> implements UacGroupIpSegmentService {

    @Resource
    private UacGroupIpSegmentMapper uacGroupIpSegmentMapper;

    /**
     * select by group id
     *
     * @param groupId the group id
     *
     * @return the list.
     */
    @Override
    public List<IpSegmentVo> selectByGroupId(Long groupId) {

        return uacGroupIpSegmentMapper.selectByGroupId(groupId);
    }

    /**
     * set ip auth
     *
     * @param ipSegmentVoList the ip segment vo list
     * @param loginAuthDto    the login auth dto
     * @return the int.
     */
    @Override
    public Integer setIpAuth(List<IpSegmentVo> ipSegmentVoList, LoginAuthDto loginAuthDto) {

        // 根据groupId删除
        Integer result = uacGroupIpSegmentMapper.deleteByGroupId(loginAuthDto.getGroupId());

        List<UacGroupIpSegment> uacGroupIpSegmentList = Lists.newArrayList();
        for (IpSegmentVo v : ipSegmentVoList) {
            UacGroupIpSegment uacGroupIpSegment = new UacGroupIpSegment();
            uacGroupIpSegment.setUpdateInfo(loginAuthDto);
            uacGroupIpSegment.setGroupId(loginAuthDto.getGroupId());
            uacGroupIpSegment.setIpSegment(v.getIpSegment());
            uacGroupIpSegment.setNumber(v.getNumber());
            uacGroupIpSegment.setRemark(v.getRemark());

            uacGroupIpSegmentList.add(uacGroupIpSegment);
        }

        return batchSave(uacGroupIpSegmentList);
    }

    /**
     * delete by group id
     *
     * @param groupId the group id
     * @return the int.
     */
    @Override
    public Integer deleteByGroupId(Long groupId) {

        return uacGroupIpSegmentMapper.deleteByGroupId(groupId);
    }

    /**
     * save uac group ip addr
     *
     * @param uacGroupIpSegment the uac group ip segment
     * @param loginAuthDto      the login auth dto
     *
     * @return the int.
     */
    @Override
    public Integer save(UacGroupIpSegment uacGroupIpSegment, LoginAuthDto loginAuthDto) {

        uacGroupIpSegment.setUpdateInfo(loginAuthDto);
        uacGroupIpSegment.setGroupId(loginAuthDto.getGroupId());

        if (uacGroupIpSegment.isNew()) {

            return uacGroupIpSegmentMapper.insertSelective(uacGroupIpSegment);
        }

        return uacGroupIpSegmentMapper.updateByPrimaryKeySelective(uacGroupIpSegment);
    }
}
